<template>
  <div class="twt-table">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-table
      :data="dataList"
      style="width: 100%"
      v-bind="childrenProps"
      height="500"
      row-key="id"
      @selection-change="handleSelect"
    >
      <el-table-column
        type="selection"
        v-if="showSelectColumn"
      ></el-table-column>
      <el-table-column
        type="index"
        width="80"
        label="序号"
        v-if="showIndexColumn"
      ></el-table-column>
      <template v-for="(item, index) in propList" :key="index">
        <el-table-column v-bind="item" show-overflow-tooltip>
          <template #default="{ row }">
            <slot :name="item.slotName" :row="row">{{ row[item.prop] }}</slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer">
      <slot name="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageInfo.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageInfo.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { sortTable } from '@/utils/hooks/tableSort'

export default defineComponent({
  name: 'twtTable',
  emits: ['handleSelect', 'update:pageInfo'],
  props: {
    dataList: {
      type: Array,
      default: () => []
    },
    dropColArr: {
      type: Array,
      default: () => []
    },
    childrenProps: {
      type: Object,
      default: () => ({})
    },
    showSelectColumn: {
      type: Boolean,
      default: true
    },
    showIndexColumn: {
      type: Boolean,
      default: false
    },
    propList: {
      type: Array,
      default: () => []
    },
    pageInfo: {
      type: Object,
      required: true
    },
    listCount: {
      type: Number,
      default: 0
    }
  },
  setup(props, { emit }) {
    // 选择框变化
    const handleSelect = (value: any) => {
      emit('handleSelect', value)
    }
    // 分页变化
    const handleSizeChange = (pageSize: number) => {
      emit('update:pageInfo', { ...props.pageInfo, size: pageSize })
    }
    const handleCurrentChange = (currentPage: number) => {
      emit('update:pageInfo', { ...props.pageInfo, page: currentPage })
    }
    // 拖拽
    // sortTable(props.dataList, ref([...props.dropColArr]))
    return {
      handleSelect,
      handleSizeChange,
      handleCurrentChange
    }
  }
})
</script>

<style scoped>
/deep/.el-table,
/deep/.el-table__empty-block {
  height: calc(100vh - 300px) !important;
}
</style>
